package TUILayer;
import java.util.Scanner;
import ControlLayer.LoanCollectionController;


/**
 * User interface class to the collection of loans
 * 
 * @author (group 1(A)) 
 * @version (14-04-2011)
 */
public class LoanUI
{
    // instance variables
    private LoanCollectionController loanCtr;
    private Scanner reader;
    private AddressBookUI addressBookUI;
    private DvdUI dvdUI;

    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI()
    {
        loanCtr = new LoanCollectionController();
        reader = new Scanner(System.in);
        addressBookUI = new AddressBookUI();
        dvdUI = new DvdUI();
    }

    /**
     * Calls a method in order to generate menu
     */
    public void start()
    {
        loanMenu();
    }
    
    /**
     * Calls the loan menu
     */
    public void loanMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise =   writeLoanMenu();
            if (choise == 1)
            {
                findLoan();
            }
            else{if(choise == 2){
                   createLoan();
                }
                else{
                    if(choise == 3){
                        //updateLoan();
                    }
                    else{
                        if(choise == 4){
                             //deleteLoan();
                        }
                        else{
                            if(choise == 5){
                                //listAllLoans();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    
    /**
     * Generates the load menu
     */
    private int writeLoanMenu()
    {
           // creates an object keyboard to read data from the keyboard
            
            System.out.println(" *** Loan Menu ***");
            System.out.println(" (1) Show loan information");
            System.out.println(" (2) Create loan");
            System.out.println(" (3) Update loan");
            System.out.println(" (4) Delete loan");
            System.out.println(" (5) Show current loan list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choise: ");
            
            int choise = reader.nextInt();
            return choise;
    }
    
    /**
     * The method reads the id of the loan and returns loan id
     * @return id Id of the loan
     */
    private int inputLoanId()
    {
        // creates an object keyboard to read data from the keyboard
        
        System.out.println("Type Loan number: ");
        int id = reader.nextInt();
        return id;
    }
    
    /**
     * The method reads the date of creation of the loan and returns date of creation
     * @return date Date of creation of the loan
     */
    private int inputDate()
    {
        // creates an object keyboard to read data from the keyboard
        
        System.out.println("Type date: ");
        int date = reader.nextInt();
        return date;
    }
    
    /**
     * The method reads the duration of the loan and returns the duration
     * @return periode The duration of the loan in number of days
     */
    private int inputPeriode()
    {
        // creates an object keyboard to read data from the keyboard
        
        System.out.println("Type periode: ");
        int periode = reader.nextInt();
        return periode;
    }
        
    /**
     * Not beeing used for the moment
     */
    private boolean inputStatus()
    {
        // creates an object keyboard to read data from the keyboard
        
        System.out.println("Type status (not available): " );
        String status = reader.next();
        boolean bstatus = true;
        if(status.equals("not available"))
        {
            bstatus = false;
        } 
        return bstatus;
    }
   
    /**
     * Finds a specified loan
     */
    private void findLoan()
    {
        System.out.println("\f");
        System.out.println("Please enter the id of the Person who has the loan:");
        int personId = reader.nextInt();
        System.out.println("Please enter the id of the Loan you want to find:");
        int id = reader.nextInt();
        
        if(loanCtr.findLoan(personId, id) != null)
        {
            System.out.println("Id: "+ loanCtr.findLoan(personId, id).getId());
            System.out.println("Title: "+ loanCtr.findLoan(personId, id).getDate());
            System.out.println("Status: "+ loanCtr.findLoan(personId, id).getStatus());
            //System.out.println("Release date: "+ loanCtr.findLoan(id).getReleaseDate());
        }
        else
        {
            System.out.println("No match found!");
        }
    }
    
    /**
     * Creates a loan
     */
    private void createLoan()
    {
        //System.out.println("\f");
        
        //String artist = inputArtist();
        //int releaseDate = inputReleaseDate();
        System.out.println("Please enter the id of the person who wants to borrow the DVD: ");
        int personId = reader.nextInt();
        System.out.println("Please enter the id of the DVD beeing borrowed: ");
        int dvdId = reader.nextInt();
        //if(addressBookUI.person(personId) && dvdUI.DVD(dvdId))
        //{
        int serialNumber = dvdUI.getCopySerialNumber(dvdId);
        int getDate = dvdUI.getCopyGetDate(dvdId);
        int getPrice = dvdUI.getCopyGetPrice(dvdId);
        int id = inputLoanId();
        int date = inputDate();
        int periode = inputPeriode();
        boolean status = true; //if status = true then the loan is active otherwise false
        //boolean status = inputStatus();
        //System.out.println("test:" + " " + id + name + address + zipCode + city + phoneNumber);
        //addressBookUI.findPerson(id).createloan();
        loanCtr.createLoan(id, date, periode, personId, dvdId, status, serialNumber, getDate, getPrice);
        //System.out.println("\f");
        System.out.println("Loan: " + id + " has been added");
        System.out.println("\n\n");
    //}
    }
}
